(define (random-in-range low high)
  (let ((random-range 100000l0)
        (range (- high low)))
    (+ low (* range (/ (random random-range) (1- random-range))))))

(define (mote-carlo trials experiment)
  (define (iter trials-remaining trials-passed)
    (cond ((= trials-remaining 0) (* 1.0l0 (/ trials-passed trials)))
          ((experiment)
           (iter (1- trials-remaining) (1+ trials-passed)))
          (else (iter (1- trials-remaining) trials-passed))))
  (iter trials 0))

(define (estimate-integral pred x1 x2 y1 y2)
  (lambda (trials)
    (mote-carlo trials (lambda ()
                         (pred (c-random-in-range x1 x2)
                               (c-random-in-range y1 y2))))))

(define  (mote-carlo-pi trials)
  (let* ((ox 0.0l0)
         (oy 0.0l0)
         (r 3.0l0)
         (expt-r (expt r 2))
         (x1 (- ox r))
         (x2 (+ ox r))
         (y1 (- oy r))
         (y2 (+ oy r)))
    (/ (* (* (- x2 x1) (- y2 y1))
          ((estimate-integral (lambda (x y)
                                (<= (+ (expt (- x ox) 2)
                                       (expt (- y oy) 2)) expt-r))
                              x1 x2 y1 y2) trials))
       expt-r)))